<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: AddUser.php 390 2010-12-25 13:15:55Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalUserRecord');
class AddUser extends PortalAdminPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->setAdminPageTitle('Add user');

		if(!$this->IsPostBack)
		{
			$this->dlLanglist->PromptText	= Prado::localize("Select");
			$this->dlLanglist->PromptValue	= 0;
			$this->dlLanglist->DataSource	= $this->DataAccess->createCommand("select culture,name from tbllanguages where active=1")->query();
			$this->dlLanglist->dataBind();

			$this->dlRole->PromptText	= Prado::localize("Select");
			$this->dlRole->PromptValue	= 0;
			$this->dlRole->DataSource	= $this->DataAccess->createCommand("select id,name from tblroles")->query();
			$this->dlRole->dataBind();
		}
	}

	public function checkUsername($sender,$param)
	{
		$userRec	= PortalUserRecord::finder()->findByname($this->Username->Text);
		$param->IsValid	= ($userRec === null ? true : false);
	}

	public function checkEmail($sender,$param)
	{
		$userRec	= PortalUserRecord::finder()->findByemail($this->Email->Text);
		$param->IsValid	= ($userRec === null ? true : false);
	}

	public function createUser($sender,$param)
	{
		if($this->IsValid)
		{
			$userRec	= new PortalUserRecord();
			if(TPropertyValue::ensureBoolean($this->Application->Parameters['AccountApproval']))
			{
				$userRec->status	= 2; //UserRecord::STATUS_PENDING;
			}
			else
			{
				$userRec->status = 0; //UserRecord::STATUS_NORMAL;
			}

			$userRec->name		= $this->Username->Text;
			$userRec->full_name	= $this->FullName->Text;
			$userRec->password	= hash("sha256",$this->Password->Text);
			$userRec->role		= $this->dlRole->SelectedValue;
			$userRec->email		= $this->Email->Text;
			$userRec->reg_time	= time();
			$userRec->sitenews	= TPropertyValue::ensureInteger($this->SiteNews->Checked);
			$userRec->culture	= $this->dlLanglist->SelectedValue;
			$userRec->save();

			$this->gotoPage('Admin.UserMan');
		}
	}
}

?>